package itesm.coler.service;

import java.util.ArrayList;

import itesm.coler.models.Autenticator;
import itesm.coler.models.User;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import itesm.coler.data.DAOFactory;
import itesm.coler.data.UserDAO;

@Produces({"application/xml", "application/json"})
@Path("/users")
public class UserResource {
	
	DAOFactory MySQLFactory = DAOFactory.getDAOFactory(DAOFactory.MySQL);
	// Create a DAO
	UserDAO userDAO = MySQLFactory.getUserDAO();
	

	 @GET
	 public ArrayList<User> getUsers() {
	    ArrayList<User> users = null;
	    users = (ArrayList<User>) userDAO.findAll();
	    return users;
	 }
	 
	 @GET
	 @Path("/type/{type}/")
	 @Produces({ "application/xml", "application/json" })
	 public ArrayList<User> getUsersByType(@PathParam("type") String type) {
	    ArrayList<User> users = null;
	    users = (ArrayList<User>) userDAO.findAllByType(type);
	    return users;
	 }
	 
	 @GET
	 @Path("/team/{teamId}/")
	 @Produces({ "application/xml", "application/json" })
	 public ArrayList<User> getUsersByTeam(@PathParam("teamId") int teamId) {
	    ArrayList<User> users = null;
	    users = (ArrayList<User>) userDAO.findAllByTeam(teamId);
	    return users;
	 }
	 
	 @GET
	 @Path("/course/{courseId}/")
	 @Produces({ "application/xml", "application/json" })
	 public ArrayList<User> getUsersByCourse(@PathParam("courseId") String courseId) {
	    ArrayList<User> users = null;
	    users = (ArrayList<User>) userDAO.findAllByCourse(courseId);
	    return users;
	 }
/*	 
	 @GET
	  @Path("{userName}")
	  public User getUser(@PathParam("userName") String userName) {
//	    return UserDAO.getUserByUserName(userName);
	  }
*/	 
	@GET
	@Produces({"application/xml", "application/json" })
	@Path("/user/{userName}/")
	public User getUsuario(@PathParam("userName") String userName) {
		User user = userDAO.findUserByUserName(userName);
		return user;
	}
	
	@PUT
	  @Path("/add")
	  @Produces("text/plain")
	  @Consumes(MediaType.APPLICATION_JSON)
	  public Response addUser(User user) {
	   try {
		   String results = null;
	       int result = userDAO.insertUser(user);
	       if (result != -1)
	          results = "User saved : " + user;
	       else
	    	  results = "User not saved: " + user;
	       return Response.status(201).entity(results).build();
	   
	   } catch (Exception sqle){
		   sqle.printStackTrace();
		   return null;
	   }
	  }
	
	@PUT
	@Path("/assignUserToCourse/{userId}/{courseId}/")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response assignUserToCourse(@PathParam("userId") String userId, @PathParam("courseId")String courseId){
		  try {
			String results = null;
			int result =userDAO.assignUserToCourse(userId, courseId);
			 if (result != -1)
		          results = "User " + userId + " assigned to " + courseId;
		       else
		    	  results = "User  " + userId + " not assigned to " + courseId;
			 return Response.status(201).entity(results).build();			   
		   } catch (Exception sqle){
			   sqle.printStackTrace();
			   return null;
		   }
	}
	
	@PUT
	@Path("/assignUsersToCourse/{userIds}/{courseId}/")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response assignUsersToCourse(@PathParam("userIds")String userIds, @PathParam("courseId")String courseId){
		  try {
			String results = null;
			int result =userDAO.assignUsersToCourse(userIds, courseId);
			 if (result != -1)
		          results = "Users assigned to " + courseId;
		       else
		    	  results = "Problem asigning users to " + courseId;
			 return Response.status(201).entity(results).build();			   
		   } catch (Exception sqle){
			   sqle.printStackTrace();
			   return null;
		   }
	}

	
	@PUT
	@Path("/modify")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response modifyUser(User user){
		  try {
			boolean result =userDAO.updateUser(user);
			System.out.println("Resultado de ModifyUser ="+result);
			return Response.status(201).entity("ok").build();	
		  } catch (Exception sqle){
			   sqle.printStackTrace();
			   return null;
		   }
	}
	
	@DELETE
	@Path("/delete/{userName}")
	public Response deleteUser(@PathParam("userName") String userName){
		userDAO.deleteUser(userName);
		return Response.status(201).entity("ok").build();
	}
	
	@PUT
	@Path("/deactivate/{userName}")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response deactivateUser(@PathParam("userName") String userName){
		userDAO.updateUserActiveStatus(userName, false);
		return Response.status(201).entity("ok").build();
	}
	
	@PUT
	@Path("/activate/{userName}")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response activateUser(@PathParam("userName") String userName){
		userDAO.updateUserActiveStatus(userName, true);
		return Response.status(201).entity("ok").build();
	}
	
	@PUT
	@Path("/setOnline/{userName}")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response setOnlineUser(@PathParam("userName") String userName){
		userDAO.updateUserOnlineStatus(userName, true);
		return Response.status(201).entity("ok").build();
	}
	
	@PUT
	@Path("/setNotOnline/{userName}")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response setNotOnlineUser(@PathParam("userName") String userName){
		userDAO.updateUserOnlineStatus(userName, false);
		return Response.status(201).entity("ok").build();
	}
	
	@GET
    @Produces({ "application/xml", "application/json" })
	@Path("/authenticateUser/{userName}/")
	public Autenticator authenticateUser(@PathParam("userName") String userName){
		User auntent = userDAO.findUserByUserName(userName);
		Autenticator auth = new Autenticator(auntent);
		return auth;
	}
	
	@POST
	@Consumes({"application/xml","application/json"})
	public Response datastoreUser(User holo){
//		UserDAO.insert(holo);
		return null;	
	}
	

}